<?php

namespace App\Lib;

class Api
{

    public static $appParams = null;
    public static $curGameId = 0;
    public static $curGameName = null;
    public static $currentUser = null;

    CONST CODE_OK = 1;
    CONST CODE_API_NOT_FOUND = 100;
    CONST CODE_PARAM_ERROR = 101;
    CONST CODE_NO_RECORD = 103;
    CONST CODE_FAILED2SAVE = 104;
    CONST CODE_SIGN_ERROR = 106;
    CONST CODE_HEADER_ERROR = 107;
    CONST CODE_EXCEED_LIMIT = 110;
    CONST CODE_CUSTOM = 111;
    CONST CODE_REPEAT_SUBMIT_ERROR = 112;

    CONST CODE_NOT_LOGGED_IN = 1001;
    CONST CODE_STEAM_ERROR = 1002;
    CONST CODE_STEAM_TRADE_URL_UNEXIST = 1003;
    CONST CODE_STEAM_TRADE_URL_FORMAT_ERROR = 1004;
    CONST CODE_STEAM_TRADE_URL_MATCH_ERROR = 1005;
    CONST CODE_STEAM_TRADE_URL_SET_FAIL = 1006;
    CONST CODE_STEAM_TRADE_URL_SERVICE_BUSY = 1007;
    CONST CODE_STEAM_TRADE_URL_LOADING_FAIL = 1008;
    CONST CODE_STEAM_TRADE_URL_INVALID_OR_INV_UNDISCLOSED = 1009;
    CONST CODE_STEAM_TRADE_URL_MOBILE_TOKEN_NEED_SEVEN_DAY = 1010;

    //存入、取回错误
    CONST CODE_FROM_TRADE_ITEM_COUNT = 2000;
    CONST CODE_FROM_TRADE_COUNT = 2001;
    CONST CODE_FROM_TRADE_FAIL = 2002;
    CONST CODE_FROM_TRADE_BOT_RETRY = 2003;
    CONST CODE_OUT_TRADE_ITEM_COUNT = 2004;
    CONST CODE_NOT_THIS_TRADE = 2004;

    CONST CODE_WALLET_CODE_NOT = 3000;
    CONST CODE_WALLET_CODE_REPEAT = 3001;
    CONST CODE_WALLET_CODE_BAD = 3002;
    CONST CODE_NOT_WALLET_CODE = 3003;
    CONST CODE_WALLET_CODE_INIT = 3004;

    public static $messages = [
        self::CODE_OK => '成功',

        self::CODE_API_NOT_FOUND => '接口不存在',
        self::CODE_PARAM_ERROR => '参数错误',
        self::CODE_NO_RECORD => '无查询数据',
        self::CODE_FAILED2SAVE => '保存数据失败',
        self::CODE_SIGN_ERROR => '签名错误',
        self::CODE_REPEAT_SUBMIT_ERROR => '请勿重复操作',

        self::CODE_NOT_LOGGED_IN => '请您先登录再进行操作',
        self::CODE_STEAM_ERROR => 'steam请求错误',
        self::CODE_STEAM_TRADE_URL_UNEXIST => '交易链接不存在',
        self::CODE_STEAM_TRADE_URL_FORMAT_ERROR => '交易链接格式错误',
        self::CODE_STEAM_TRADE_URL_MATCH_ERROR => '交易链接与当前STEAM账号不匹配',
        self::CODE_STEAM_TRADE_URL_SET_FAIL => '交易链接设置失败',
        self::CODE_STEAM_TRADE_URL_SERVICE_BUSY => '机器人系统繁忙',
        self::CODE_FROM_TRADE_BOT_RETRY => '机器人系统繁忙，请稍后再试',
        self::CODE_STEAM_TRADE_URL_LOADING_FAIL => 'STEAM交易链接加载失败, 请稍后尝试',
        self::CODE_STEAM_TRADE_URL_INVALID_OR_INV_UNDISCLOSED => '交易链接无效或库存未公开',
        self::CODE_STEAM_TRADE_URL_MOBILE_TOKEN_NEED_SEVEN_DAY => 'STEAM令牌手机认证器至少7天',
        self::CODE_FROM_TRADE_ITEM_COUNT => '建议单次存入物品不要超过500个',
        self::CODE_FROM_TRADE_COUNT => '为了保证交易安全，不能同时发起超过1个存入报价',
        self::CODE_OUT_TRADE_ITEM_COUNT => '建议单次取出物品不要超过200个',
        self::CODE_FROM_TRADE_FAIL => '报价发起失败,请过5分钟再尝试',
        self::CODE_NOT_THIS_TRADE => '无效报价',

        self::CODE_WALLET_CODE_BAD => '充值失败，请稍后尝试',
        self::CODE_WALLET_CODE_NOT => '您输入的 Steam 钱包充值码无效。重新输入时请仔细核对字符。',
        self::CODE_WALLET_CODE_REPEAT => '您输入的 Steam 钱包充值码已被兑换。Steam 客服无法向您发放新的充值码。',
        self::CODE_NOT_WALLET_CODE => '无效的记录',
        self::CODE_WALLET_CODE_INIT => '充值中',

    ];

    public static function msg($code)
    {
        return isset(self::$messages[$code]) ? self::$messages[$code] : 'undefined message';
    }

    public static function jsonOutput($code, $msg = null, $result = null, $callback = null)
    {
        $msg = $msg ? $msg : self::msg($code);
        if (!$result) {
            $result = (object)null;
        }
        $data = [
            'code' => $code,
            'msg' => $msg,
            'result' => $result,
        ];
        if ($callback && preg_match('~^[a-z_][a-z0-9_]*$~i', $callback)) {
            return response()->json($data)->setCallback($callback);
        } else {
            return response()->json($data);
        }
    }

    public static function error($code, $msg = '', $result = null, $callback = null)
    {
        return self::jsonOutput($code, $msg, $result);
    }

    public static function success($result = null, $msg = '请求成功', $callback = null)
    {
        return self::jsonOutput(self::CODE_OK, $msg, $result, $callback);
    }
}